//
//  LKMainTabBarController.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/22.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit

class LKMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        let LKTabBar = LKTabbar()
        LKTabBar.closure =
            {
                //添加发布微博的视图
                let composeView = LKComposeView()
                composeView.show(target: self)
//                let window = UIApplication.shared.keyWindow
//                window?.addSubview(composeView)
        }
        self.setValue(LKTabBar, forKey: "tabBar")
        

    }
    
    private func addChildViewControllers()
    {
        addChildViewController(vc: LKHomeTableController(), imageName: "tabbar_home", title: "首页",index:0)
        addChildViewController(vc: LKMessageTableController(), imageName: "tabbar_message_center", title: "消息",index:1)
        addChildViewController(vc: LKDiscTableController(), imageName: "tabbar_discover", title: "发现",index:2)
        addChildViewController(vc: LKProfileTableController(), imageName: "tabbar_profile", title: "我的",index:3)
    }
    
    private func addChildViewController(vc:UIViewController,imageName:String,title:String,index:Int)
    {
//        //实例化子控制器
//        let homeVC = LKHomeTableController ( )
        //设置属性
        vc.navigationItem.title = title
        vc.tabBarItem.title = title
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange,NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .selected)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        let navC = LKBaseNavController(rootViewController: vc)
        self .addChildViewController(navC)
    }
    //当选中一个tabbarItem的时候就会调用这个方法
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //遍历子视图,找出被选中的那个按钮,然后对它的子视图进行遍历,找到相应的视图进行放大和缩小操作
        var index: Int = 0
        for subview in tabBar.subviews
        {
            if subview .isKind(of: NSClassFromString("UITabBarButton")!) {
                if item.tag == index {
                    //找到了点击的item
                    //遍历uitabbarItem
                    for target in subview.subviews
                    {
                        if target .isKind(of: NSClassFromString("UITabBarSwappableImageView")!){
                            //找到目标进行缩放
                            //先缩小
                            target.transform = CGAffineTransform.init(scaleX: 0.4, y: 0.4)
                            //再方法
                            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
                                target.transform = CGAffineTransform.init(scaleX: 1, y: 1)
                                }, completion: nil)
                        }
                    }
                }
                index += 1
            }
        }
    }
}
